//****************************************************************************
//Copyright (C) 2005 Adobe Systems, Inc. All Rights Reserved.
//The following is Sample Code and is subject to all restrictions on
//such code as contained in the End User License Agreement accompanying
//this product.
//****************************************************************************

/**
 * MenuItem, Version 1
 * Creates Menu items with highlighted states for a Flash Lite 2 application
*/

class MenuItem {
	
	//---------------------------------------------- Properties
	
	private var menuItem_mc:MovieClip;		// used to store menu item movieClip instance
	private var classInstance:String;			// used to store this class' instance name
	private var viewer:Object;				// used to store class instance of viewer
	
	//---------------------------------------------- Methods
	
	/**
	 * MenuItem Constructor
	 * 
	 * @param menuLabel		the text label for the menu item
	 * @param xPos			the x position to place the menu item at
	 * @param yPos			the y position to place the menu item at
	 * @param classType		the class type we want to make a new instance of
	 * @param mediaURL		the URL of the new media we want to load in the new class instance
	 * @param target			the main menu clip to create our new menu item clip in
	 * @param depth			the depth of our new menu item clip
	 */
	public function MenuItem (menuLabel:String, 
							xPos:Number, 
							yPos:Number, 
							classType:String, 
							mediaURL:String, 
							target:MovieClip, 
							depth:Number) {
		menuItem_mc = target.attachMovie("item_mc", "menuItem_mc" +  depth, depth, {_x:xPos, _y:yPos});
		this.menuItem_mc = menuItem_mc;						// set instance var
		this.menuItem_mc.classInstance = "menu" + depth;		// set instance var
		setLabel(menuLabel);
		setClassType(classType);
		setMediaURL(mediaURL);
		setHighlight(false);
		setOnPress();
	}	
	
	/**
	 * set the label of this menuItem instance
	 * 
	 * @param label	the text string we want to display
	 */
	 private function setLabel(menuLabel:String):Void {
	 	menuItem_mc.label_txt.text = menuLabel;	
	 }
	
	/**
	 * set the classType of this menuItem instance
	 * 
	 * @param classType	the new classType we wish to instantiate
	 */
	 private function setClassType(classType:String):Void {
	 	menuItem_mc.classType = classType;	
	 }
	 
	 /**
	 * get the classType for this menuItem instance
	 * 
	 * @return the classType passed to the constructor, for this instance
	 */
	 public function getClassType():String {
	 	return menuItem_mc.classType;	
	 }
	 
	 /**
	  * set the path to the media this menuItem will load
	  * 
	  * @param mediaURL	the URL of the media we wish to load
	  */
	 private function setMediaURL(mediaURL:String):Void {
	 	menuItem_mc.mediaURL = mediaURL;
	 }
	 
	/**
	 * set this instance's onPress event
	 * 
	 */
	private function setOnPress():Void {
		menuItem_mc.onPress = function():Void {
			this._parent[classInstance].eventCall();
		}
	}
	
	/**
	 * this is the method called by onPress, it should be the method overwritten if extended
	 * 
	 */
	 public function eventCall():Void {
		if (System.capabilities.hasDataLoading == 1) {		// FL2.0 device specific sys cap
			var MIME:MIMEChecker = new MIMEChecker();		// checks other FL2 device sys caps
			var mime:String;
		 	switch(menuItem_mc.classType) {
				case "ImageViewer":
					mime = MIME.getMIME(menuItem_mc.mediaURL);			
					if (MIME.checkImageType(mime)) {
						viewer = new ImageViewer(_root, 100, 5.2, 60.5, 168, 108, 1, 0x333333, true);
						viewer.loadImage(menuItem_mc.mediaURL);
					} else {
						viewer = new ImageViewer(_root, 100, 5.2, 60.5, 168, 108, 1, 0x333333, true);
						viewer.createMessage("Your handset does not support\nthis file type.");	
					}
					break;
				case "VideoViewer":
					mime = MIME.getMIME(menuItem_mc.mediaURL);			
					if (MIME.checkVideoType(mime)) {
						viewer = new VideoViewer(_root, 100, 0.5, 44.5, 176, 144, 1, 0x333333, true);
						viewer.loadVideo(menuItem_mc.mediaURL);
					} else {
						viewer = new ImageViewer(_root, 100, 5.2, 60.5, 168, 108, 1, 0x333333, true);
						viewer.createMessage("Your handset does not support\nthis file type.");
					}
					break;
				case "SoundPlayer":
					mime = MIME.getMIME(menuItem_mc.mediaURL);
					if (MIME.checkAudioType(mime)) {
						viewer = new SoundPlayer(_root, 100, 5.2, 60.5, 168, 108, 1, 0x333333, true);
						viewer.loadSound(menuItem_mc.mediaURL);
					} else {
						viewer = new ImageViewer(_root, 100, 5.2, 60.5, 168, 108, 1, 0x333333, true);
						viewer.createMessage("Your handset does not support\nthis file type.");
					}
					break;
			}
			_root.viewer = viewer;						// store the Object reference on the root instead of locally
		}
	 }
	 
	 /**
	  * destroy the viewer object, removing it from the screen
	  * 
	  */
	 public function destroyViewer():Void {
	 	_root.viewer.destroy();
	  	delete _root.viewer;
	 }
	  
	/**
	 * turn the highlight state of this menuItem on and off
	 * 
	 * @param state	turn the movieClip _visible property to 1 or 0
	 */
	public function setHighlight(state:Boolean):Void {
		menuItem_mc.highlight_mc._visible = state;
	}
	
	/**
	 * set the enabled property of this menuItem instance to on or off
	 * 
	 * @param state	enable or disable the item
	 */
	public function enableClip(state:Boolean):Void {
		menuItem_mc.enabled = state;
	 }
	 
}